package com.yzc.android.widget.banner;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import java.util.List;

import com.squareup.picasso.Picasso;
import com.yzc.android.beans.BannerInfoBean;
import com.yzc.android.utils.DensityUtil;

public class SliderBannerManager {

    private SliderBanner mSliderBanner;
    private InnerAdapter mBannerAdapter = new InnerAdapter();

    private View.OnClickListener mClickItemListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    };

    public SliderBannerManager(SliderBanner sliderBanner) {
        mSliderBanner = sliderBanner;
        sliderBanner.setAdapter(mBannerAdapter);
    }

    public void play(List<BannerInfoBean> list) {
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetChanged();
        mSliderBanner.setDotNum(list.size());
        mSliderBanner.beginPlay();
    }

    private class InnerAdapter extends BannerAdapter {
        private List<BannerInfoBean> mDataList;

        public void setData(List<BannerInfoBean> data) {
            mDataList = data;
        }

        public BannerInfoBean getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.get(getPositionForIndicator(position));
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.size() == 0) {
                return 0;
            }
            return position % mDataList.size();
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            final ImageView imageView = new ImageView(layoutInflater.getContext());
            BannerInfoBean item = getItem(position);
            String url = item.imgUrl.trim();
            Picasso.with(layoutInflater.getContext())
                    .load(url)
                    .resize(DensityUtil.getScreenWidth(), 0)
                    .into(imageView);
            imageView.setTag(item);
            imageView.setOnClickListener(mClickItemListener);
            return imageView;
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            return Integer.MAX_VALUE;
        }
    }
}
